/**
 * @author 王晓伟
 * @version 1.0
 * @description 为了定义radiobox,checkbox,combox这三个组件中的键值对内容而特别定义的editorgrid。
 */
Ext.ns('Ext.ux.grid'); 
Ext.ux.grid.AssembleEditorGrid = Ext.extend(Ext.grid.EditorGridPanel,{
	anchor:'100% 50%',
    data:new Array(),
    isCheckBox:false,
    isComBox:false,
    viewConfig: {
        forceFit: true
    },
    storeItems:new Array(),
    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
    clicksToEdit: 1,
    checkSelectSingle: false,
    stripeRows:true,
	initComponent:function(){
		this.tbar=[{
	    	text:'添加',
	    	handler:function(){
	    		var _grid = this.findParentBy(function(p){
					if(p.getXType() == 'uxassembleeditorgrid' ){
						return true;
					}
				});
				var Plant = _grid.getStore().recordType;
				var p = new Plant({
					value:'实际的值',
					text:'显示的值',
					columnwidth:100,
					order:1
				});
				  _grid.stopEditing();				  
				  if(_grid.getStore().data.length > 1){
				  	_grid.getStore().insert(_grid.getStore().getCount(), p);	
				  }else if(_grid.getStore().data.length == 1){
				  	_grid.getStore().insert(1, p);	
				  }else{
				  	_grid.getStore().insert(0, p);
				  }	              
	              _grid.startEditing(0, 0);
	    	}
	    },{
	    	text:'删除',
	    	handler:function(){
	    		var _grid = this.findParentBy(function(p){
					if(p.getXType() == 'uxassembleeditorgrid' ){
						return true;
					}
				});
				if(_grid.getSelectionModel().getCount()>0){
					Ext.Msg.show({
						icon: Ext.MessageBox.INFO,	
							 title:Ext.systemTitle,
	   						 msg: '是否删除？',
	   						 buttons:Ext.Msg.YESNO,
	   						 fn:function(_buttonid){
	   							if(_buttonid == 'yes'){
	   								var   _r =  _grid.getSelectionModel().getSelected();
									_grid.getStore().remove(_r);		
	   							}		 	
	   						 }
					});
				}else{
					Ext.Msg.show({
						 icon: Ext.MessageBox.WARNING,	
						 title:Ext.systemTitle,
   						 msg: '请选择一行数据。',
   						 buttons:Ext.Msg.OK
					});
				}
	    	}
	    }],
	    this.columns = this.columns || [
        {header: "实际的值", sortable: true, dataIndex: 'value',editor: new Ext.form.TextField({allowBlank: false,vtype : 'Blank'})},
        {header: "显示的值", sortable: true, dataIndex: 'text',editor: new Ext.form.TextField({allowBlank: false,vtype : 'Blank'})},
        {header: "是否选中", dataIndex: 'checked',editor: new Ext.form.Checkbox({}),
         renderer:function(value){
         	return value?'选中':'未选中';
         }	
        }],
    	this.on('afteredit',function(_object){
    		if(this.checkSelectSingle == true && _object.field == 'checked'){
    			var _rownumber = _object.row ;
    			var _store =  this.getStore();
    			_store.each(function(_r){
    				_r.set('checked',false);
    			});
    			_store.getAt(_rownumber).set('checked',true);
    			
    		}
    	},this),
		Ext.ux.grid.AssembleEditorGrid.superclass.initComponent.call(this);
		this.store = new Ext.data.SimpleStore({
			fields:['value','text','checked'],
			data:this.data
		});		
	},
	getStore:function(){
		return this.store;
	},
	updateStore:function(){
		this.store.commitChanges();
	},
	getStoreItems:function(_name){
		if(this.isCheckBox){
			var _itemsarray = new Array();
			var _namestart = 1;
			this.store.each(function(_r){
				var _temp = new Object();
				_temp.boxLabel = _r.get('text');
				_temp.value = _r.get('value');
				_temp.checked = _r.get('checked');
				_temp.name = _name+_namestart;
				_itemsarray.push(_temp);
				_namestart++;
			},this);
			this.storeItems = _itemsarray;
			return this.storeItems;
		}if(this.isComBox){
			var _itemsarray = new Array();
			this.store.each(function(_r){
				var _temp = new Object();
				_temp.boxLabel = _r.get('text');
				_temp.value = _r.get('value');
				_temp.checked = _r.get('checked');
				_itemsarray.push(new Array(_temp.value,_temp.boxLabel,_temp.checked));
			},this);
			this.storeItems = _itemsarray;
			return this.storeItems;	
		}else{
			var _itemsarray = new Array();
			this.store.each(function(_r){
				var _temp = new Object();
				_temp.boxLabel = _r.get('text');
				_temp.value = _r.get('value');
				_temp.checked = _r.get('checked');
				_temp.name = _name;
				_itemsarray.push(_temp);
			},this);
			this.storeItems = _itemsarray;
			return this.storeItems;
		}
	},
	getData:function(){
		var _totlearay = new Array();		
		this.store.each(function(_r){
			var _temparray = new Array();
			_temparray[0] = _r.get('value');
			_temparray[1] = _r.get('text');
			_temparray[2] = _r.get('checked');
			_totlearay.push(_temparray);
		});
		return _totlearay;
	}
});
Ext.reg('uxassembleeditorgrid',Ext.ux.grid.AssembleEditorGrid);